package com.imooc.controller;

import com.imooc.common.utils.common.lang.StringUtils;
import com.imooc.pojo.BO.UserBO;
import com.imooc.pojo.Users;
import com.imooc.service.UserService;
import com.imooc.utils.CookieUtils;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.utils.JsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author lihongyu
 * @CreateTime 2021-08-06 12:49
 * @Description 用于注册登录的相关接口
 */
@Api(value = "注册登录", tags = {"用于注册登录的相关接口"})
@RestController
@RequestMapping("/passport")
public class PassportController {

    @Resource
    private UserService userService;


    /**
     * 校验用户名是否存在
     * @param username 用户名
     * @return 存在/不存在
     */
    @ApiOperation(value = "用户名是否存在", notes = "用户名是否存在", httpMethod = "GET")
    @GetMapping("/usernameIsExist")
    public IMOOCJSONResult validateUserNameIsExist(@RequestParam String username){
        if(StringUtils.isBlank(username)){
            return IMOOCJSONResult.errorMsg("用户名不能为空!");
        }
        boolean result = userService.validateUserNameIsExist(username);
        if(result){
            return IMOOCJSONResult.errorMsg("用户名已存在!");
        }
        return IMOOCJSONResult.ok();
    }

    /**
     * 用户注册
     * @param userBO 用户注册封装对象实体
     * @return 成功/失败
     */
    @ApiOperation(value = "用户注册", notes = "用户注册", httpMethod = "POST")
    @PostMapping("/regist")
    public IMOOCJSONResult registerUser(@RequestBody UserBO userBO,
                                        HttpServletRequest request,
                                        HttpServletResponse response){
        String username = userBO.getUsername();
        String password = userBO.getPassword();
        String confirmPwd = userBO.getConfirmPassword();
        if(StringUtils.isEmpty(username) ||
                StringUtils.isEmpty(password)||
                StringUtils.isEmpty(confirmPwd)){
            return IMOOCJSONResult.errorMsg("用户名或密码不能为空!");
        }
        // 1. 查询用户名是否存在
        boolean isExist = userService.validateUserNameIsExist(username);
        if (isExist) {
            return IMOOCJSONResult.errorMsg("用户名已经存在");
        }
        // 2. 密码长度不能少于6位
        if (password.length() < 6) {
            return IMOOCJSONResult.errorMsg("密码长度不能少于6");
        }
        // 3. 判断两次密码是否一致
        if (!StringUtils.equals(password,confirmPwd)) {
            return IMOOCJSONResult.errorMsg("两次密码输入不一致");
        }
        // 4. 实现注册
        Users userResult = userService.createUser(userBO);
        setNullProperty(userResult);
        CookieUtils.setCookie(request, response, "user",
                JsonUtils.objectToJson(userResult), true);

        // TODO 生成用户token，存入redis会话
        // TODO 同步购物车数据

        return IMOOCJSONResult.ok();
    }

    /**
     * 用户登录
     * @param userBO 用户注册封装对象实体
     * @return 成功/失败
     */
    @ApiOperation(value = "用户登录", notes = "用户登录", httpMethod = "POST")
    @PostMapping("/login")
    public IMOOCJSONResult login(@RequestBody UserBO userBO,
                                        HttpServletRequest request,
                                        HttpServletResponse response){
        String username = userBO.getUsername();
        String password = userBO.getPassword();
        if(StringUtils.isEmpty(username) ||
                StringUtils.isEmpty(password)){
            return IMOOCJSONResult.errorMsg("用户名或密码不能为空!");
        }
        // 1. 实现登录
        Users userResult = userService.queryUserLogin(userBO);
        if(userResult == null){
            return IMOOCJSONResult.errorMsg("用户名或密码错误!");
        }
        setNullProperty(userResult);
        CookieUtils.setCookie(request, response, "user",
                JsonUtils.objectToJson(userResult), true);

        // TODO 生成用户token，存入redis会话
        // TODO 同步购物车数据

        return IMOOCJSONResult.ok(userResult);
    }

    /**
     * 用户注销
     * @param userId 用户编码
     * @return 成功/失败
     */
    @ApiOperation(value = "用户注销", notes = "用户注销", httpMethod = "POST")
    @PostMapping("/logout")
    public IMOOCJSONResult logout(@RequestParam String userId,
                                 HttpServletRequest request,
                                 HttpServletResponse response){
        CookieUtils.deleteCookie(request,response,"user");
        // TODO 生成用户token，存入redis会话
        // TODO 同步购物车数据

        return IMOOCJSONResult.ok();
    }



    private void setNullProperty(Users userResult) {
        userResult.setPassword(null);
        userResult.setMobile(null);
        userResult.setEmail(null);
        userResult.setCreatedTime(null);
        userResult.setUpdatedTime(null);
        userResult.setBirthday(null);
    }

}
